package com.gwnet.shop.pool;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
public class MyThreadPool {


    int corePoolSize = 5; // 核心线程数
    int maximumPoolSize = 10; // 最大线程数
    long keepAliveTime = 1L; // 非核心线程空闲存活时间
    TimeUnit unit = TimeUnit.SECONDS; // 时间单位
    BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(20); // 任务队列
    ThreadFactory threadFactory = Executors.defaultThreadFactory(); // 线程工厂
    RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy(); // 拒绝策略

    ExecutorService executorService = new ThreadPoolExecutor(
            corePoolSize,
            maximumPoolSize,
            keepAliveTime,
            unit,
            workQueue,
            threadFactory,
            handler
    );


    @Bean
    public ExecutorService taskExecutor() {
        // 创建一个固定大小的线程池
        return executorService;
    }
}
